package com.liao.work.ui.activity.bmi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.liao.work.R;
import com.liao.work.util.SetActionBarTitle;

public class BMITestActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_height;
    private EditText et_weight;
    private RadioButton rb_boy;
    private RadioButton rb_girl;
    private Button btn_commit;
    private Button btn_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b_m_i_test);
        initView();
        SetActionBarTitle.setTitle(getSupportActionBar(), false, "健康测试");
    }

    private void initView() {
        et_height = (EditText) findViewById(R.id.et_height);
        et_weight = (EditText) findViewById(R.id.et_weight);
        rb_boy = (RadioButton) findViewById(R.id.rb_boy);
        rb_girl = (RadioButton) findViewById(R.id.rb_girl);
        btn_commit = (Button) findViewById(R.id.btn_commit);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);

        btn_commit.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_commit:
                submit();
                break;
            case R.id.btn_cancel:
                et_height.setText("");
                et_weight.setText("");
                break;
        }
    }

    private void submit() {
        // validate
        String height = et_height.getText().toString().trim();
        if (TextUtils.isEmpty(height)) {
            Toast.makeText(this, "请输入身高", Toast.LENGTH_SHORT).show();
            return;
        }

        String weight = et_weight.getText().toString().trim();
        if (TextUtils.isEmpty(weight)) {
            Toast.makeText(this, "请输入体重", Toast.LENGTH_SHORT).show();
            return;
        }

        String sex = rb_boy.isChecked() ? "男" : "女";
        try {
            Intent intent = new Intent(this, BMIResultActivity.class);
            float v = Float.parseFloat(weight);
            float v1 = Float.parseFloat(height);
            intent.putExtra("weight", v);
            intent.putExtra("height", v1);
            intent.putExtra("sex", sex);
            startActivity(intent);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            Toast.makeText(this, "请输入有效的数字!", Toast.LENGTH_SHORT).show();
        }

    }
}
